﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data;
using shfa.snomtastic.data.business;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eEventLogItem : EditBase
    {

        #region Public Members

        public bool IsNewItem
        {
            get { return (ActiveItemId == Guid.Empty); }
        }

        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        public AppConfig ac = new AppConfig();


        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            if (ActiveItemId != Guid.Empty)
            {
                bindItem();
            }
            
        }

        #endregion

        #region Helpers

        private void bindItem()
        {

            EventLogController elc = new EventLogController();
            EventLogInfo eli = elc.SelectEventLog(ActiveItemId);

            if (eli!=null)
            {
                txtEventDate.Text = common.LongDateTime(eli.EventDate);
                txtEventType.Text = eli.EventType;
                txtMacAddress.Text = eli.MacAddress;
                txtIpAddress.Text = eli.IpAddress;
                txtUserAgent.Text = eli.UserAgent;
                txtEventMessage.Text = eli.EventMessage;

                if (eli.UserId!=Guid.Empty)
                {
                    UserController uc = new UserController();
                    UserInfo ui = uc.SelectUser(eli.UserId);
                    if (ui!=null)
                    {
                        txtUserName.Text = ui.UserName;
                    }
                }

            }


        }

        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            EventLogController elc = new EventLogController();
            EventLogInfo eli;
            switch (e.ActionName)
            {
                case "Phone":
                    eli = elc.SelectEventLog(ActiveItemId);
                    if (eli != null)
                    {
                        if (eli.PhoneId != Guid.Empty)
                        {
                            RaiseNavEvent(this, new NavEventArgs("ePhone", eli.PhoneId));
                        }
                    }
                    break;
                case "User":
                    eli = elc.SelectEventLog(ActiveItemId);
                    if (eli != null)
                    {
                        if (eli.UserId != Guid.Empty)
                        {
                            RaiseNavEvent(this, new NavEventArgs("eUser", eli.UserId));
                        }
                    }

                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eEventLog", ActiveItemId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Phone", "Phone Details", "~/images/icons/phone.gif"));
                al.Add(new ActionInfo("User", "User Details", "~/images/icons/user.png"));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));

                return al;
            }
        }

        #endregion


    }
}